var express = require('express');
// const { cinemas } = require('../public/scr/mock/data/cinemas');
var router = express.Router();
//获取数据库api中get方法
const { get, add, remove,getData ,cinemasListAll,getOne,change,findOne,updata} = require("../api/cinemas");

router.get('/', async function (req, res, next) {
    // console.log(1);
    const { curPage, eachPage } = req.query;
    const data = await get({ curPage, eachPage })
    // console.log(1);
    res.send(data)
});

router.post('/', async function (req, res, next) {
    const { name, address, phone, status } = req.body;
    console.log({ name, address, phone, status });
    const data = await add({ name, address, phone, status })
});
// 通过id查找影院
router.get('/findOne/:_id', async function (req, res, next) {
    const { _id } = req.params;
    console.log(_id);
    const data = await findOne(_id);
    res.send(data)
});

router.patch('/:_id', async function (req, res, next) {
    const { _id } = req.params;
    const data = req.body;
    // console.log(data);
    // 传值更新
    await updata({ _id }, data);
    // 返回更新的整条数据
    const result = await findOne({ _id });
    res.send(result)
});
// 删除
router.post('/remove', async function (req, res, next) {
    const { _id } = req.body;
    await remove({ _id });
    res.send({ status: true })
});
//  修改
router.put("/change", async function (req, res, next) {
    const newData = req.body;
    await change(newData);
    res.send({ status: true });
})
//排片、放映厅获取影院的数据
router.get('/getData',async function(req, res, next) {
    const data=await getData()
    res.send(data)
});

module.exports=router;
// 获取所有的影院  排片用的
router.get('/cinemasListAll', async function (req, res, next) {
    const data = await cinemasListAll();
    res.send(data);
});
router.get("/:_id", async function (req, res, next) {
    const { _id } = req.params;
    const data = await getOne(_id);
    res.send(data);
});

//查询、排片、放映厅获取影院的数据
router.get('/getData',async function(req, res, next) {
    const data=await getData()
    res.send(data)
});
module.exports = router;


